package com.dameng.jianyouquan.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.dameng.jianyouquan.R;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;

public class LocalRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //item类型
    private static final int ITEM_TYPE_HEADER1 = 1;
    private static final int ITEM_TYPE_HEADER2 = 2;
    private static final int ITEM_TYPE_HEADER3 = 3;
    private static final int ITEM_TYPE_CONTENT = 4;

    private LayoutInflater mLayoutInflater;
    private Activity context;
    private int count = 10;


    public LocalRecycleViewAdapter(Activity activity) {
        this.mLayoutInflater = LayoutInflater.from(activity);
        context = activity;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER1) {
            return new Header1Holder(mLayoutInflater.inflate(R.layout.item_home_head1, parent, false));
        } else if (viewType == ITEM_TYPE_HEADER2) {
            return new Header2Holder(mLayoutInflater.inflate(R.layout.item_home_head2, parent, false));
        } else if (viewType == ITEM_TYPE_HEADER3) {
            return new Header3Holder(mLayoutInflater.inflate(R.layout.item_home_head3, parent, false));
        } else if (viewType == ITEM_TYPE_CONTENT) {
            return new ContentHolder(mLayoutInflater.inflate(R.layout.item_home_list, parent, false));
        }
        return null;
    }


    class Header1Holder extends RecyclerView.ViewHolder {
        Banner banner;

        private Header1Holder(@NonNull View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }

    private class Header2Holder extends RecyclerView.ViewHolder {
        private Header2Holder(@NonNull View itemView) {
            super(itemView);
        }
    }


    private class Header3Holder extends RecyclerView.ViewHolder {
        private Header3Holder(@NonNull View itemView) {
            super(itemView);
        }
    }


    private class ContentHolder extends RecyclerView.ViewHolder {
        private ContentHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == ITEM_TYPE_HEADER1) {
            Header1Holder holder1 = (Header1Holder) holder;
            ArrayList<DataBean> dataBeans = new ArrayList<>();
            dataBeans.add(new DataBean(R.mipmap.banner1));
            dataBeans.add(new DataBean(R.mipmap.banner2));
            dataBeans.add(new DataBean(R.mipmap.banner3));
            holder1.banner.setAdapter(new BannerImageAdapter<DataBean>(dataBeans) {
                @Override
                public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(data.imageUrl)
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                            .into(holder.imageView);
                }
            }).setIndicator(new CircleIndicator(context));
        } else if (itemViewType == ITEM_TYPE_CONTENT) {
            ContentHolder holderview = (ContentHolder) holder;
            holderview.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnClick(view);
                }
            });
        }
    }

    OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void OnClick(View v);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }


    private class DataBean {

        DataBean(int imageUrl) {
            this.imageUrl = imageUrl;
        }

        int imageUrl;

        public int getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(int imageUrl) {
            this.imageUrl = imageUrl;
        }
    }

    @Override
    public int getItemCount() {
        return count + 3;
    }




    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_HEADER1;
        } else if (position == 1) {
            return ITEM_TYPE_HEADER2;
        } else if (position == 2) {
            return ITEM_TYPE_HEADER3;
        } else {
            return ITEM_TYPE_CONTENT;
        }
    }





}
